package com.ac.app.web.action.pick;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;
import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;

import com.caac.core.config.OrderClazz;
import com.caac.core.config.QueryClass;
import com.caac.core.dao.CriteriaCallback;
import com.caac.core.dm.OrderProp;
import com.caac.core.util.Page;
import com.caac.core.web.action.BasePageViewAction;
import com.caac.core.web.action.PageViewForward;
import com.caac.core.web.form.BaseForm;
import com.ac.app.dm.PickRecord;
import com.ac.app.dm.PickUrl;
import com.ac.app.web.form.PkRecordSearchForm;
import com.ac.app.web.view.PkRecordView;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright AgileCentury 2008</p>
 * <p>Company: AgileCentury </p>
 * @author <a href="mailto:ac@agilecentury.com">Agile</a>
 * @version 1.0
 */

public class SearchPkBackRecordAction extends BasePageViewAction {
    private static final String QUERY_NAME = "pkRecordQuery"; 
    private static final String ORDER_NAME = "pkRecordOrderBk";
    private static final String VIEW_NAME = "pkRecordView";

    protected PageViewForward doPageViewExecute(ActionMapping mapping,
                                                BaseForm form,
                                                HttpServletRequest request,
                                                HttpServletResponse response,
                                                int pageNo,
                                                int pageSize) throws Exception {
        PkRecordSearchForm myForm = (PkRecordSearchForm) form;
        QueryClass qc = getQuery(QUERY_NAME);
        String whereStr="1=1 ";
        
        String key  = request.getParameter("key");
        if(key!=null && key.length()>0 ){
        	String[] keys = key.split(" ");
        	for(int i=0;i<keys.length;i++){
        	   whereStr += " and content like '%"+ keys[i]+"%'";
        	}
        	whereStr = "("+whereStr+")";
        }
        CriteriaCallback cc = getCc(whereStr);
        if(myForm.getOrderstr()==null || "".equals(myForm.getOrderstr())){
        	myForm.setOrderFlag(1);
        	myForm.setOrderstr("prjBegdate");
        }
        
        request.setAttribute("orderstr", myForm.getOrderstr());
        request.setAttribute("orderflag", myForm.getOrderFlag());
        OrderClazz oc =  getOc(myForm.getOrderstr(),myForm.getOrderFlag());
        
        Page page = new Page(getBaseManager().getCount(PickRecord.class, qc,
                myForm, myForm.isAnd(),cc), pageNo, pageSize);
        List dmList = getBaseManager().getObjects(PickRecord.class, qc, myForm,
                myForm.isAnd(),oc, cc, page);
        List vList = new ArrayList();
        for (int i = 0; i < dmList.size(); i++) {
            PickRecord dm = (PickRecord) dmList.get(i);
            PkRecordView dmView = new PkRecordView();
            dmView.setIsLock(this.getUrlKind(dm.getUrl()));
            dmView.setPickRecord(dm);
            vList.add(dmView);
        }
        return new PageViewForward(forward(mapping,"success"), vList, page,
                getSelViewProps(VIEW_NAME), null, null);
    }

    private static CriteriaCallback getCc(final String whereStr) {
        return new CriteriaCallback() {
            public void execute(Criteria criteria) {
                criteria.add(Expression.sql(whereStr));
            }
        };
    }
    
    private String getUrlKind(String url){
    	List list = this.getBaseManager().getObjects(PickUrl.class, "url='"+url+"'");
    	if(list.size()>0)
    		return ((PickUrl)list.get(0)).getUrlKind();
    	else
    		return "";
    }
    
    private OrderClazz getOc(String orderStr,int orderFlag){
    	List list = new ArrayList();   
    	if(orderStr!=null && !orderStr.equals("")){
    		OrderProp op = new OrderProp();
    		op.setPropName(orderStr);
    		op.setGroupName("pkRecordOrderBk0");
    		if(orderFlag==1){
        		op.setAsc(false);
    		}else{
        		op.setAsc(true);
    		}
    		list.add(op);
    	}
    	return new OrderClazz(list);
    }
}
